//时间戳的转换法方法
function getLocalTime(nS) {
   return new Date(parseInt(nS) * 1000).toLocaleString().replace(/:\d{1,2}$/,' ');     
}     







var userId = localStorage.getItem("userId");
function aa(){
	localStorage.setItem("userId","");
	$(".hideBox .loginaBox").show();
	$(".hideBox .inforBox").hide();
}
$(".headerBox").load("header.html",function(){
	//用于判断的登陆状态
	function getState(){
		
		if(localStorage.getItem("userId")){
			
			$(".hideBox .loginaBox").hide();
			
			$(".hideBox .inforBox").show();
			$.ajax({
					type:"POST",
					url:ptms.url.common.mySelf.mySelf,
					data:{
						userId:userId,
					},
					success:function(data){
						
						var html = "";
						html += '<li><a href="myTrain.html">我的学习</a></li>' + 
									'<li><a href="#">' + data.data.znname + '</a></li>' + 
									'<li class="quit" onclick="aa()">退出</li>'
							
						$(".informationB").html(html);
					}
					
				})
			
		}else{
			
			$(".hideBox .loginaBox").show();
			$(".hideBox .inforBox").hide();
		}
	}
	getState();
});
$(".footer").load("footer.html");

// 用于获取课程的id 
var courseId = window.location.search.split("=")[1];
//用于判断用户是否已经登陆了；





//课程名称  
coursesChnName = null;
//所在班级
classInfor = null;
function getLocalTime(nS) {     
    return new Date(parseInt(nS)).toLocaleString().replace(/:\d{1,2}$/,' ');     
} 

//页面的主框架的接口
$.ajax({ 
	type:"POST",
	url:ptms.url.common.courseManger.courseItem,
	async:true,
	data:{
		"id":courseId  //这里的id是课程id
	},
	success:function(data){ //主框架的接口调用
		coursesChnName = data.coursesChnName;
		var html = "";
		html += '<div class="subjectImgBox container">' + 
						'<div class="row">' + 
							'<img src="' + ptms.address2 + data.coursesPhotoUrl + '" class="img-responsive"/>' + 
							'<div class="addClassButton">开始<br/>上课</div>' + 
						'</div>' + 
					'</div>' + 
					'<div class="container">' + 
						'<div class="row classInformation">' + 
							'<div class="pull-left leftDiv">' + 
								'<p class="firstP">参与人数:</p>' + 
								'<p class="secondP">' + data.userCount + '</p>' + 
								'<p class="thirdP">课程讲师</p>' + 
								'<ul class="fourP exportAA">' + 
//									'<li>葛特立</li>' + 
//									'<li>葛丽倩</li>' + 
								'</ul>' + 
								'<p class="fivep">课程学习</p>' + 
								'<p class="sixP">班级作业</p>' + 
							'</div>' + 
							'<div class="pull-left rightDiv">' + 
								'<p class="classTitle pl">' + data.coursesChnName + '</p>' + 
								'<div>' + 
									'<div class="classContent pl pr pt pb">' + data.coursesIntro + '</div>' + 
								'</div>' + 
								'<div class="classContentBox clearfix">' + 
									'<div class="classContentLeft pull-left">' + 
										'<div class="classList">' + 
										'</div>' + 
									'</div>' + 
									'<div class="classContentRight pull-left">' + 
										'<div class="peopleInformation">' + 
										'</div>' + 
										'<div class="peopleClassInformation">' + 
											
										'</div>' + 
										'<div class="classAnnounce">班级公告</div>' + 
										'<div class="classAnnounceContent">' + 
											
										'</div>' + 
										'<div class="classAnnounce">任务要求</div>' + 
										'<div class="classAnnounceWork">' + 
											'<p class="workP testPress"></p>' +
											'<p class="workP workPress"></p>' + 
											'<p class="fontP fontaaP">任务规则说明</p>' + 
											'<p class="fontP">专题测试中分子代表正确题目累计数，分母代表课程全部课程题目数.主管作业中分子代表已获得评分的作业数量，分母代表全部主观题的数量</p>' + 
										'</div>' + 
									'</div>' + 
								'</div>' + 
								'<div></div>' + 
							'</div>' + 
						'</div>' +
					'</div>'
		$(".main").html(html);
		
		//开始上课的按钮功能
		$(".addClassButton").click(function(){
			window.location.href = "trainClass.html?coursesId=" + courseId;
		})
		
		
		
		//课程专家的接口
		$.ajax({
			type:"GET",
			url:ptms.url.common.other.exportList,
			data:{
				coursesId:courseId,
				offset:1,
				limit:2
			},
			success:function(data){
				var html = "";
				$.each(data, function(i,o) {
					html += "<li>" + o.znname + "</li>"
				});
				$(".exportAA").html(html);
			}
			
		})
		
		
		
		
		//课程下的专题的接口
		//用于判断获取到哪个专题
		function getSubject(i){
			$.ajax({
				type:"GET",
				url:ptms.url.common.courseManger.courseItemSubjectList,
				data:{
					coursesId:courseId,
				},
				success:function(data){
					var newData = data[i];
					var subid = newData.id;
					
				    var html = "";
					var arr = ["一","二","三","四","五","六","七","八","九","十","十一","十二","十三","十四","十五","十六","十七","十八","十九","二十","二十一","二十二","二十三","二十四","二十五"];
					html += '<div class="classItem">' + 
								'<div class="classT">' + 
									'<span class="firstP">专题' + arr[i] + ':</span><span class="secondP">' + newData.spcSubName + '</span>' + 
									'<ul class="pull-right classType">'+
										'<li>作</li>' + 
										'<li>测</li>' + 
										'<li>资</li>' + 
									'</ul>' +
								'</div>' +
								'<div class="classWork">' + 
									'<p class="classWorkTitle  bgdian">专题作业</p>' + 
									'<div class="classWorkList">' + 
									'</div>' + 
								'</div>'+
								'<div class="classTest">' + 
									'<p class="classTestTitle bgdian">' + 
										'<span></span>专题测试' + 
									'</p>' + 
									'<div class="testList">' + 
									'</div>' + 
									'<p class="submitTest"><span>提交答案</span></p>' + 
								'</div>' + 
								'<div class="classResource">' + 
									'<p class="classResourceTitle bgdian">' + 
										'<span></span>专题资源' + 
									'</p>' + 
									'<div class="classResourceList">' + 
									'<div>' + 
								'<div>' + 
							'</div>'
							
					$(".classList").html(html);
					//用于专题下的作业接口
					$.ajax({
						"type":"POST",
						url:ptms.url.common.courseManger.courseItemSubjectItemWork,
						data:{
							subId:subid,
							type:0
						},
						success:function(data){
							var html = "";
							$.each(data, function(i,o) {
								html += '<div class="classWorkItem" data-workId="' + o.id + '">' + 
												'<p class="classWorkItemTitle">' + (i+1) + o.tistTitle + '</p>' + 
												'<p class="classWorkItemContent">' + o.testContent + '</p>' + 
												'<div class="classWorkItemAdd clearfix">' + 
													'<div class="classWorkItemAddAffix pull-left">'+
														'<p class="classWorkItemAddAffixTitle">附件</p>' + 
														'<ul class="classWorkItemAddAffixList">' + 
//															'<li><span class="flw">中外服装设计史中外服装设计史中外服装设计史中外服装设计史中外服装设计史中外服装设计史外服装设计史中外服装设计史中外服装设计史中外服装设计史中外服装外服装设计史中外服装设计史中外服装设计史中外服装设计史中外服装</span><span class="down iconfont">&#xe633;</span></li>
//															<li><span class="flw">中外服装设计史中外服装设计史中外服装设计史中外服装设计史中外服装设计史中外服装设计史外服装设计史中外服装设计史中外服装设计史中外服装设计史中外服装外服装设计史中外服装设计史中外服装设计史中外服装设计史中外服装</span><span class="down iconfont">&#xe633;</span></li>
														'</ul>' + 
													'</div>' + 
													'<div class="classWorkItemAddWork pull-left">' + 
														'<p class="classWorkItemAddWorkTitle">提交作业</p>' +
														'<ul class="classWorkItemAddWorkList">' + 
//															<li><span class="flw">中外服装设计史中外服装设计史中外服装设计史中外服装设计史中外服装设计史中外服装设计史外服装设计史中外服装设计史中外服装设计史中外服装设计史中外服装外服装设计史中外服装设计史中外服装设计史中外服装设计史中外服装</span><span class="down iconfont">&#xe600;</span></li>
//															<li><span class="flw">中外服装设计史中外服装设计史中外服装设计史中外服装设计史中外服装设计史中外服装设计史外服装设计史中外服装设计史中外服装设计史中外服装设计史中外服装外服装设计史中外服装设计史中外服装设计史中外服装设计史中外服装</span><span class="down iconfont">&#xe600;</span></li>
														'</ul>' + 
													'</div>' + 
												'</div>' + 
											'</div>'
							});
							$(".classWorkList").html(html);
							
							//这里用闭包实现自执行达到循环调取接口的目的
							for(var i = 0; i < $(".classWorkList .classWorkItem").length; i++){
								(function(){
									var workId = $(".classWorkList .classWorkItem").eq(i).attr("data-workId");
									
									//专题作业的附件的展示列表;
									$.ajax({
										type:"POST",
										url:ptms.url.common.courseManger.courseItemAddAffixList,
										data:{
											"testId":workId
										},
										success:function(data){
											console.log(data)
											var html = "";
											$.each(data.rows, function(i,o) {
												html += '<li><span class="flw">' + o.resourseName +'</span><a href=' + ptms.address2 + '/oss/file/download?fileId=' + o.resourseCode + ' class="down iconfont">&#xe633;</a></li>'
											});
											$(".classWorkItemAddAffixList").eq(i).html(html);
											
										},
										async:true
									});
								})(i)
							}
							
							//点击作业的提交按钮  //这里需要专题id:subId
							$(".classWorkItemAddWorkTitle").click(function(){
								var workAA = $(this).parents(".classWorkItem").find(".classWorkItemTitle").html().substring(1);
								
								var workId1 = $(this).parents(".classWorkItem").attr("data-workid");
								var str =  encodeURI("uploadWork.html?subId=" + workId1 + "&courseId=" + courseId + "&coursesChnName=" + coursesChnName + "&classInfor=" + classInfor + "&workName=" + workAA + "");
								window.location.href = str;
							})
							
							
							for(var i = 0; i < $(".classWorkList .classWorkItem").length; i++){
								(function(){
									var workId = $(".classWorkList .classWorkItem").eq(i).attr("data-workId");
									//专题作业下的已上传作业的列表接口
									$.ajax({
										type:"POST",
										url:ptms.url.common.courseManger.uploadWorkList,
										data:{
											subId:workId,
											pageNo:0,
											pageSize:2
										},
										success:function(data){
											
										}
									})
									
								})(i)
							}
						}
					})
					
					//专题测试接口
					$.ajax({
						type:"POST",
						url:ptms.url.common.courseManger.courseItemSubjectItemWork,
						data:{
							subId:subid,
							type:1
						},
						async:true,
						success:function(data){
							var html  = '';
							$.each(data,function(i,o){
								
								html +='<div class="testItem" testId="' + o.id + '">' + 
										'<p class="testItemTitle">' + (i+1) + o.testContent + '</p>' + 
										'<div class="testItemContent">' + 
												'<div class="testItemContentItem col-md-6 flw">' + 
													'<input type="radio" name="' + i + '" value="1"/>' + o.options[0].optionValue + '.<span>' + o.options[0].optionContent + '</span>' + 
												'</div>' + 
												'<div class="testItemContentItem col-md-6 flw">' + 
													'<input type="radio"  name="' + i + '" value="2"/>' + o.options[1].optionValue + '.<span>' + o.options[1].optionContent + '</span>' + 
												'</div>' + 
												'<div class="testItemContentItem col-md-6 flw">' + 
													'<input type="radio"  name="' + i + '" value="2"/>' + o.options[2].optionValue + '.<span>' + o.options[2].optionContent + '</span>' + 
												'</div>' + 
												'<div class="testItemContentItem col-md-6 flw">' + 
													'<input type="radio"  name="' + i + '" value="4"/>' + o.options[3].optionValue + '.<span>' + o.options[3].optionContent + '</span>' + 
												'</div>' + 
										'</div>' + 
									'</div>'
							})
							$(".testList").html(html);
							
							
							//专题测试的提交按钮
							$(".submitTest span").click(function(){
								var checkResult = "[";
								for(var i = 0; i < $(".testItem").length; i++){
									
									var id = $(".testItem").eq(i).attr("testId")
									var aa = $("input[name='" + i + "']:checked").val();
									
									checkResult += "{testId" + ":" + id + "," + "testResult" + ":" + aa + "},";
									
//									[{"testId":"1","optionId":"1"},{"testId":"2","optionId":"3"}]
								}
								checkResult += "]"
								$.ajax({
									type:"POST",
									url:ptms.url.common.courseManger.courseItemSubjectItemTestSubmit,
									data:{
										"userId":10,
										"testResult":checkResult
									},
									success:function(data){
										
										
									},
									async:true
								});
								
								
							})
							
							
						}
					});
					
					//专题资源的展示的列表接口  这里需要专题的id
					$.ajax({
						type:"POST",
						url:ptms.url.common.courseManger.courseItemSubjextItemResourceList,
						data:{
							subId:subid
						},
						success:function(data){
							var html3 = "";
							$.each(data.rows, function(i,o) {
								
								html3 += '<div class="classResourceItem" data-code="">' + 
										'<p><span class="iconfont">' + o.resourseName +'</span></p>' + 
										'<p class="firstSpan flw"></p>' + 
										'<p class="secondSpan iconfont"><span ></span></p>' + 
										'<p class="thirdSpan iconfont"><a class="iconfont" href="' + ptms.address2 + '/oss/file/download?fileId=' + o.resourseCode + '">&#xe633;</a></p>' + 
									'</div>'
							});
							$(".classResourceList").html(html3);
						}
					})
					
				}
			})
		}//课程下专题的结尾
		
		getSubject(0)
		
		//右边的接口  
		
		//个人信息的接口
		$.ajax({
			type:"POST",
			url:ptms.url.common.mySelf.mySelf,
			data:{
				userId:userId
			},
			success:function(data){
				var html = "";
				html += '<img src="../images/微信图片_20170518134826-恢复的-恢复的_03.png" class="img-responsive"/>' + 
						'<span class="peopleName">王子</span>'
				$(".peopleInformation").html(html);
			}
			
		})
		
		//个人班级信息的接口
		$.ajax({
			type:"GET",
			url:ptms.url.common.classManger.classInformation,
			data:{
				userId:userId,
				courseId:courseId
			},
			success:function(data){
				classInfor = data.data.className;
				var html = "";
				html += '<p>班级：<span>' + data.data.className + '</span></p>' + 
							'<p>辅导员：' + data.data.classTeacherName + '</p>' + 
							'<p>班级人数：' + data.data.classStudentsNumber + '人</p>'
				$(".peopleClassInformation").html(html);
			}
		})
		
		
		
		
		//专题测试进度的接口
		$.ajax({
			type:"POST",
			url:ptms.url.common.courseManger.testPress,
			data:{
				"coursesId":courseId,
				"userId":userId
				
			},
			success:function(data){
				
				var html = "";
				html += '专题测试（<span class="firstSpan">' + data.data[0] + '</span>/<span>' + data.data[1] + '</span>）'
				$(".testPress").html(html);
			}
		})
		
		//专题作业的进度接口
		$.ajax({
			type:"POST",
			url:ptms.url.common.courseManger.myWorkPress,
			data:{
				coursesId:courseId,
				"userId":userId
			},
			success:function(data){
				var html = "";
				html += '专题作业（<span class="firstSpan">' + data.data[0] + '</span>/<span>' + data.data[1] + '</span>）'
				$(".workPress").html(html);
			}
		})
		
		//班级公告的接口
		$.ajax({
			type:"GET",
			url:ptms.url.common.classManger.classAnnounce,
			data:{
				courseId:courseId,
				"userId":userId
			},
			
			success:function(data){
				var html1 = "";
				if(data.data.rows.length < 0){
					html1 = "主人有点懒，还没有上传公告！"
				}else{
					html1 += '<p class="classAnnounceContentTitle">' + data.data.rows[0].noticeTitle + '<span>' +  getLocalTime(data.createTime) + '</span></p>' + 
							'<p class="classAnnounceContentContent">' + data.data.rows[0].noticeContent + '</p>' 
				}
				$(".classAnnounceContent").html(html1);			
			}
		})
		
	
	
	
	
	//主框架的接口结尾
	},
	error:function(data){
		console.log(data)
	}
});
